import {$http} from '@bana/miniapp-common-util'

Component({
  externalClasses: ['img-cls', 'cls', 'v-cls', 'm-cls'],
  properties: {
    imgCode: {
      type: [String, Array],
      value: ''
    },
    imgSrc: {
      type: String,
      value: ''
    },
    imgAttachList: {
      type: Array,
      value: []
    },
    code: {
      type: [String, Array],
      value: ''
    },
    url: {
      type: String,
      value: ''
    },
    w: {
      type: Number,
      value: 100
    },
    h: {
      type: Number,
      value: 100
    },
    playIng: {
      type: Boolean,
      value: false
    },
    key: {
      type: String,
      value: 'video_key'
    },
    cls: {
      type: String,
      value: ''
    }
  },
  data: {
    style: '',
    iconStyle: '',
    videoUrl: '',
    _code: ''
  },
  observers: {
    'url, code': function () {
      this.initUrl()
    },
    playIng() {
      this.playVideo()
    }
  },
  lifetimes: {
    attached() {
      this.setData({
        style: 'height:' + this.properties.h + 'rpx;width:' + this.properties.w + 'rpx',
        iconStyle: 'top:' + (this.properties.h - 80) / 2 + 'rpx;left:' + (this.properties.w - 80) / 2 + 'rpx'
      })
      this.initUrl()
    }
  },
  methods: {
    playVideo() {
      if (this.properties.playIng) {
        const videoContext = wx.createVideoContext(this.properties.key, this)
        videoContext.play()
      }
    },
    initUrl() {
      if (this.properties.url) {
        this.setData({
          videoUrl: this.properties.url
        })
      } else if (this.properties.code) {
        this.findUrl()
      }
    },
    findUrl() {
      const _this = this
      // var localData = wx.getStorageSync(this.properties.key);
      const localData = null
      if (localData) {
        _this.setData({
          videoUrl: localData.url ? localData.url : ''
        })
      } else {
        let _code = ''
        if (this.properties.code instanceof Array) {
          if (this.properties.code.length) {
            _code = this.properties.code[0]
          } else {
            return
          }
        } else {
          _code = this.properties.code
        }
        $http.post('attach/find/' + _code).then(({resultData}) => {
          // wx.setStorageSync(_this.properties.key, resultData);
          _this.setData({
            // eslint-disable-next-line promise/always-return
            videoUrl: resultData.url ? resultData.url : ''
          })
        }).catch(e => {
          console.log(e)
        })
      }
    },
    doPlay() {
      const _this = this
      wx.getNetworkType({
        success(res) {
          const networkType = res.networkType
          // wifi下直接播放
          if (networkType === 'wifi') {
            _this.triggerEvent('playVideo')
          } else if (networkType === '5g' || networkType === '4g' || networkType === '3g' || networkType === 'unknown' || networkType === '2g') {
            const msg = networkType === 'unknown' ? '您正在使用未知网络类型，是否继续播放' : '您正在使用移动网络，是否继续播放'
            wx.showModal({
              title: '提示',
              content: msg,
              success(res) {
                if (res.confirm) {
                  _this.triggerEvent('playVideo')
                }
              }
            })
          } else if (networkType === 'none') { // 没有网络
            wx.showModal({
              title: '提示',
              content: '请检查网络连接'
            })
          }
        }
      })
    }
  }
})
